//
//  ChatViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/13.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "ChatViewController.h"
#import <AVOSCloud.h>
#import <RongIMKit/RongIMKit.h>
#import "ChatMainViewController.h"
@interface ChatViewController ()
@end

@implementation ChatViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *leftIrem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftIrem;
    
    UIBarButtonItem *rightItem1 = [[UIBarButtonItem alloc]initWithTitle:@"删除好友" style:UIBarButtonItemStylePlain target:self action:@selector(RightItemAction1:)];

    UIBarButtonItem *rightItem2 = [[UIBarButtonItem alloc]initWithTitle:@"添加好友" style:UIBarButtonItemStylePlain target:self action:@selector(RightItemAction2:)];
    if (self.key==1) {
        self.navigationItem.rightBarButtonItem = rightItem1;
    }else{
        self.navigationItem.rightBarButtonItem = rightItem2;
    }
    
    
    
   }
-(void)leftItemAction:(UIBarButtonItem *)sender{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark删除操作
-(void)RightItemAction1:(UIBarButtonItem*)sender{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:@"确认删除该好友?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        //对方好友列表删除操作
        AVQuery *query = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Friend%@",self.name]];
        AVObject *obj = [query getFirstObject];
        if (obj==nil) {
            obj = [AVObject objectWithClassName:[NSString stringWithFormat:@"Friend%@",self.name]];
        }
        NSMutableArray *arr = [NSMutableArray array];
        if ([obj objectForKey:[NSString stringWithFormat:@"Friend%@",self.name]]!=nil) {
            arr = [obj objectForKey:[NSString stringWithFormat:@"Friend%@",self.name]];
        }
        [arr removeObject:[AVUser currentUser].username];
        [obj setObject:arr forKey:[NSString stringWithFormat:@"Friend%@",self.name]];
        [obj saveInBackground];
        //已方好友列表删除操作
        AVQuery *query1 = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
        AVObject *obj1 = [query1 getFirstObject];
        if (obj1==nil) {
            obj1 = [AVObject objectWithClassName:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
        }
        NSMutableArray *arr1 = [NSMutableArray array];
        if ([obj1 objectForKey:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]]!=nil) {
            arr1 = [obj1 objectForKey:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
        }
        [arr1 removeObject:self.name];
        [obj1 setObject:arr1 forKey:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
        [obj1 saveInBackground];
        ChatMainViewController *chatVC = [[ChatMainViewController alloc]init];
        [self.navigationController presentViewController:chatVC animated:YES completion:nil];
  
    }];
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    [self.navigationController presentViewController:alertController animated:YES completion:nil];
    
    
    
    
    
   
    
    
}
#pragma mark 添加好友操作
-(void)RightItemAction2:(UIBarButtonItem*)sender{
    //判断是否已经是好友
    AVQuery *query1 = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Friend%@",self.name]];
    AVObject *obj1 = [query1 getFirstObject];
    if (obj1==nil) {
        obj1 = [AVObject objectWithClassName:[NSString stringWithFormat:@"Friend%@",self.name]];
    }
    
    NSMutableArray *arr1 = [NSMutableArray array];
    if (obj1) {
         arr1 = [obj1 objectForKey:[NSString stringWithFormat:@"Friend%@",self.name]];
    }
    NSLog(@"%@*********",arr1);

    
      int i = 0;
    for (NSString  *key in arr1) {
        if ([key isEqualToString:[AVUser currentUser].username]) {
            i++;
            NSLog(@"%d",i);
        }
    }
    if (i>0) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:@"你们已经是好友了请不要重复添加~" preferredStyle:UIAlertControllerStyleAlert];
     UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];  
        [alertController addAction:cancelAction];
        [self.navigationController presentViewController:alertController animated:YES completion:nil];

    }else{
        AVQuery *query = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Post%@",self.name]];
        AVObject *obj = [query getFirstObject];
        if (obj==nil) {
            obj = [AVObject objectWithClassName:[NSString stringWithFormat:@"Post%@",self.name]];
        }
        
        NSMutableArray *arr = [NSMutableArray array];
        if ([obj objectForKey:[NSString stringWithFormat:@"Post%@",self.name]]!=nil) {
            arr = [obj objectForKey:[NSString stringWithFormat:@"Post%@",self.name]];
        }
        int j=0;
        for (NSString *str in arr) {
            if ([str isEqualToString:[AVUser currentUser].username]) {
                j++;
            }
        }
        if (j>0) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:@"您已经发送过请求了请不要重复发送~" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:cancelAction];
            [self.navigationController presentViewController:alertController animated:YES completion:nil];
        }else{
            [arr addObject:[AVUser currentUser].username];
            [obj setObject: arr forKey:[NSString stringWithFormat:@"Post%@",self.name]];
            [obj saveInBackground];
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"恭喜" message:@"好友请求发送成功~" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:cancelAction];
            [self.navigationController presentViewController:alertController animated:YES completion:nil];

        }
    }
    
    
    
    

}






@end
